<?php
namespace app\home\controller;
use think\Db;
class Listzu extends Base
{
    /**
     * 租房数据
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function homePage(){
        extract(input());
        $search = input('param.search/a');
        $page = input('param.page');
        if(!$page){
            $pages = 0;
        }else{
            $pages = $page-1;
        }
        if(!session('?my_location_city_id')){
            $shi = 1002;
        }else{
            $shi = session('my_location_city_id');
        }
        $search = $this->search($search, $page);
        //搜索条件
        $map = [];
        //1、区域筛选
        if($search[0] != ""){
            $map['h_qu'] = $search[0];
        }else{
            $map['h_shi'] = $shi;
        }
        //2、价格筛选:不限都为空，第一档的最小传1
        if($search[1] != ""){
            $jiage = [
                '0,50000000',
                '1000,2000',
                '2000,3000',
                '3000,4000',
                '4000,5000',
                '5000,6000',
                '6000,7000',
                '7000,8000',
                '8000,50000000'
            ];
            $map['h_price'] = array('between',$jiage[$search[1]]);
        }
        //3、面积筛选：也判断一下
        if($search[2] != ""){
            $mianji = [
                '0,50000000',
                '0,50',
                '50,80',
                '80,110',
                '110,150',
                '150,50000000'
            ];
            $map['h_mianji'] = array('between',$mianji[$search[2]]);
        }
        //4、户型
        if($search[3] != ""){
            $map['h_shis'] = $search[3];
        }
        //5、装修
        if($search[4] != ""){
            $map['h_fix'] = $search[4];
        }
        //6、朝向
        if($search[5] != ""){
            $map['h_face'] = $search[5];
        }
        //7、标签
        if($search[6] != ""){
            if($search[6] == -1){
                $map['h_sign'] = -1;
            }else{
                if($search[6] != 100){
                    $sign = trim($search[6],',');
                    $signdata = explode(',',$sign);
                    if(count($signdata) > 1){
                        $we = array();
                        foreach($signdata  as $vo){
                            $we[] = ['like',"%,$vo,%"];
                        }
                        $map['h_sign'] = [$we,'OR'];
                    }else{
                        $map['h_sign'] = ['like','%,'.$signdata[0].',%'];
                    }
                }
            }
        }
        //8、来源
        if($search[7] != ""){
            $map['h_user_type'] = $search[7];
        }
        //9、看房时间
        if($search[8] != ""){
            $map['h_kan'] = $search[8];
        }
        //10、更多（房屋配置）
        if($search[9] != ""){
            if($search[9] == -1){
//                $map['h_more'] = -1; //以前是默认展示没有
                $map['h_more'] = ''; //现在展示选空的
            }else{
                if($search[9] != 100){
                    $more = trim($search[9],',');
                    $moredata = explode(',',$more);
                    if(count($moredata) > 1){
                        $we1 = array();
                        foreach($moredata  as $vo){
                            $we1[] = ['like',"%$vo%"];
                        }
                        $map['h_more'] = [$we1,'OR'];
                    }else{
                        $map['h_more'] = ['like','%'.$moredata[0].'%'];
                    }
                }else{
                    $map['h_more'] = ['<>',''];
                }
            }
        }

        $map['h_type'] = 1;
        $map['h_status'] = 1;
        $data = Db::table('up_house h')
            ->join ('up_area a','h.h_shi = a.district_id','LEFT')
            ->where($map)
            ->field(['h_id','h_title','h_xiaoqu','h_price','h_shis','h_tings','h_mianji','h_img','h_type','h_sign','district'])
            ->order('h_order DESC,h_listtime DESC')
            ->paginate(25);
        $qus = Db::table('up_area')
            ->where('level',3)
            ->where('pid',$shi)
            ->field('district_id,district')
            ->select();
        //----------------------------
        $types = [10=>'短租', 11=>'拎包入住',12=>'精装修',13=>'配套完善',14=>'可供暖',15=>'停车方便'];
        //----------------------------
        $fetch = [
            'data'=>$data,'pages'=>$pages,'search'=>$search,
            'types'=>$types, 'qus'=>$qus
        ];
        return $this->fetch ('list/listzu',$fetch);
    }
}